package com.ruoyi.common.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.constant.GlobalConstants;
import com.ruoyi.common.utils.AesGcm256Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class PreventionConfig {
    // 市局代理服务地址
    public static String ip = "http://172.24.0.49/send";
//    public static String ip = "https://ydbg.yjglj.shanghai.gov.cn:8089/api";
//    public static String ip = "https://www.baosight-aq.com:8080/api";

    //申能集团环境
    public static String ipJT = "http://172.16.2.157:81";
     //联调环境
//    public static String ipJT = "http://172.24.0.49/sendOne";
//    public static String ipJT = "http://192.168.93.157:81";
    
    public static String unitJT = "/api/SendDataInserInfoSystem.aspx";
    public static String secureJT = "/api/InserDataIntoSystem.aspx";


    public static String getToken = "/v1/data/auth/credentials/accessToken";
    public static String unit = "/v1/data/receive/receiveUnitList";
    public static String unitStop = "/v1/data/receive/receiveUnitStopList";
    public static String event = "/v1/data/receive/receiveEventList";
    public static String control = "/v1/data/receive/receiveControlMeasuresList";
    public static String task = "/v1/data/receive/receiveMeasuresTaskList";
    public static String record = "/v1/data/receive/receiveMeasuresTaskRecordList";
    public static String investigation = "/v1/data/receive/receiveDangerInvestigationList";
    public static String error = "/v1/data/receive/getRequestLog";
    public static String getInfo = "/v1/data/receive/getLog";
    public static final String[] IMEE = {"864668066946962",
            "866337069063195",
            "866337065639964",
            "868938071036109",
            "861523064019929",
            "864668066763763",
            "864668067002963",
            "866203061473693",
            "864668066749242",
            "866337069039849",
            "864668066723528"
    };

    // 新增：随机获取IMEE数组中的一个元素
    public static String getRandomIMEE(String mobileMe) {
        if (StrUtil.isNotEmpty(mobileMe) && mobileMe.length()>5) {
            return mobileMe;
        }

        return IMEE[new Random().nextInt(IMEE.length)];
    }

    public static String token() {
        String url = ip + getToken;
        Map<String, String> map = new HashMap<>();
        map.put("app_id", "06e9f82867b44277b296801e160b876e");
        map.put("app_secret", "a53c1b4a05514058b1bc2c4f7808afcf");
        map.put("company_code", GlobalConstants.COMPANY_CODE);
        HttpResponse execute = HttpRequest
                .post(url)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .timeout(3000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return null;
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            return null;
        }
        JSONObject obj = jsonObject.getJSONObject("obj");
        String accessToken = obj.getStr("access_token");
        return accessToken;
    }

    public static String pushData(String data, String url) {
//        log.info("推送市局端 url:{} 请求参数:{}", url, data);
        String token = token();
        if (StrUtil.isEmpty(token)) {
            log.error("获取token信息异常");
            return "获取token信息异常";
        }
        Map<String, String> map = new HashMap<>();
        map.put("data", AesGcm256Util.encrypt(data));
        HttpResponse execute = HttpRequest
                .post(ip + url)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .contentType(" application/json")
                .header(" UniTrust-Token", token)
                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
                .timeout(5000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return "请求失败";
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            log.error("推送市局端错误 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送市局端错误 url:{} 返回信息:{}请求参数:{}", ip + url, jsonObject.toString(), data);
        } else {
            log.info("推送市局端成功 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送市局端成功 url:{} 返回信息:{} 请求参数:{}", ip + url, jsonObject.toString(), data);
        }
    }


    public static String pushDataJT(String data, String url) {
        log.info("推送申能集团端 url:{} 请求参数:{}", url, data);
        HttpResponse execute = HttpRequest
                .post(ipJT + url)
                .charset("UTF-8")
                .body(data)
                .contentType(" application/json")
                .timeout(5000)
                .execute();

        log.info("推送申能集团端 url:{} 返回信息:{}", url, execute.body());

        int status = execute.getStatus();
        if (status != 200) {
            return "请求失败";
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("status");
        if (code != 0) {
            log.error("推送申能集团错误 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送申能集团错误 url:{} 返回信息:{}请求参数:{}", ipJT + url, jsonObject.toString(), data);
        } else {
            log.info("推送申能集团成功 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送申能集团成功 url:{} 返回信息:{} 请求参数:{}", ipJT + url, jsonObject.toString(), data);
        }
    }


    public static String errorInfo(List<String> ids, String url) {
        String token = token();
        if (StrUtil.isEmpty(token)) {
            log.error("获取token信息异常");
            return "获取token信息异常";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("errorIds", ids);
        HttpResponse execute = HttpRequest
                .post(ip + url)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .contentType(" application/json")
                .header(" UniTrust-Token", token)
                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
                .timeout(3000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return "请求失败";
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            log.error("查询推送信息错误 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送市局端错误 url:{} 返回信息:{}", url, jsonObject.toString());
        } else {
            log.info("查询推送信息成功 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送市局端成功 url:{} 返回信息:{}", url, jsonObject.toString());
        }
    }

    public static String getInfo(String batchId, String url) {
        String token = token();
        if (StrUtil.isEmpty(token)) {
            log.error("获取token信息异常");
            return "获取token信息异常";
        }
        List<String> list = new ArrayList<>();
        list.add(batchId);
        Map<String, Object> map = new HashMap<>();
        map.put("batchId", list);
        HttpResponse execute = HttpRequest
                .post(ip + url)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .contentType(" application/json")
                .header(" UniTrust-Token", token)
                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
                .timeout(3000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return "请求失败";
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            log.error("查询推送信息错误 url:{} 返回信息:{}", ip + url, jsonObject.toString());
            return StrUtil.format("推送市局端错误 url:{} 返回信息:{}", url, jsonObject.toString());
        } else {
            log.info("查询推送信息成功 url:{} 返回信息:{}", ip + url, jsonObject.toString());
            return StrUtil.format("推送市局端成功 url:{} 返回信息:{}", url, jsonObject.toString());
        }
    }
}
